<div class="in forms">
<form id="postForm" name="postForm" method="post" action="/admin/post?oper=save" onsubmit="javascript: return false;">
	<input type="hidden" name="id" value="${post.id}">
	<input type="hidden" name="saveStatus" value="publish">
	<input type="hidden" name="type" value="${post.type!'post'}">
	<input type="hidden" name="pageOrder" value="${post.pageOrder!'0'}">
	
	<div class="sidebar left" style="width: 730px; border-right: 1px solid #EBEBEB;">
		<label class="comment" id="title_label">// 文章标题</label>
		<input type="text" name="title" class="box" placeholder="这里输入标题" value="${post.title!''}">
	  	<p>
			<label class="comment" id="content_label">// 文章内容, 尽情书写吧</label>
			<textarea name="content" rows="1" cols="1" class="box" style="width: 720px; height: 320px;">${post.content!''}</textarea>
		</p>
		<p>
			<label class="comment">// 摘要：文章列表时用到, 可不填</label>
			<textarea name="abstracts" rows="1" cols="1" class="box" style="width: 720px; height: 150px;">${post.abstracts!''}</textarea>
		</p>
		<label class="comment">// 文章别名：一个对搜索引擎更友好的提示, 可不填(部分主题支持)</label>
		<input type="text" name="seoTitle" class="box" placeholder="这里输入文章别名" value="${post.seoTitle!''}">
	</div>
	
	<div class="right" style="width: 310px;">
		<label class="comment" id="cat_label">// 分类目录</label>
		<select name="category" class="box2">
			<option value="0">不分类</option>
			<#list blog.getCategories()! as cat>
				<option value="${cat.id}"<#list post.cats! as pcat><#if (pcat.id==cat.id)> selected</#if></#list>>
					<@com.repeat cat.levelNum - 1>&nbsp;&nbsp;</@com.repeat>${cat.name}
				</option>
			</#list>
	  	</select>
	  	
	  	<p>
			<label class="comment">// 文章标签：方便查找</label>
			<input type="text" name="newtag" class="box2" style="width: 205px;" placeholder="新添加标签">
			<input type="button" class="com_btn2" onclick="addnewtag()" value="添加">
			<ul class="float_ul" id="tags">
				<#list post.tags! as tag>
					<li class="r">
						<input type="hidden" name="tag" value="${tag.name}">
						<a class="del" onclick="del(this)">&nbsp;</a>&nbsp;${tag.name}
					</li>
				</#list>
			</ul>
			<div class="clear"></div>
			
			<a href="javascript:tags()">从常用标签中选择</a>
			<ul class="float_ul" id="taglist" style="display: none;">
				<#list blog.getTags()! as tag>
				<li class="r" style="font-size: ${13 + tag.postCount % 30}px;" onclick="insertNewTag('${tag.name}')">${tag.name}</li>
				</#list>
			</ul>
			<div class="clear"></div>
		</p>
		
		<br>
	  	<p>
			置顶：
			<label><input type="radio" name="top" value="1"<#if (1==post.top)> checked</#if>>是</label>
			<label><input type="radio" name="top" value="0"<#if (0==post.top)> checked</#if>>否</label>
		</p>
		<p>
			公开度：
			<#assign status="${post.status!'public'}">
			<#if status=='draft' || status=='auto'>
				<#assign status="public">
			</#if>
			<label><input type="radio" name="status" value="public"<#if ('public'==status)> checked</#if>>公开</label>
			<label><input type="radio" name="status" value="private"<#if ('private'==status)> checked</#if>>私有</label>
			<label><input type="radio" name="status" value="protected"<#if ('protected'==status)> checked</#if>>密码保护</label>
			<input type="text" name="password" class="box2" value="${post.password!''}" placeholder="访问密码" style="<#if ('protected'!=status)>display: none;</#if>width: 280px;">
		</p>
		
		<p>
			评论：
			<#assign commentStatus="${post.commentStatus!'open'}">
			<label><input type="radio" name="commentStatus" value="open"<#if ('open'==commentStatus)> checked</#if>>打开</label>
			<label><input type="radio" name="commentStatus" value="close"<#if ('close'==commentStatus)> checked</#if>>关闭</label>
		</p>
		
		<img src="/admin/images/loading.gif" id="loading" style="display: none;">
		<label id="tip">
		</label>
		
		<br>
		<p align="right">
			<input type="button" class="com_btn2" value="存为草稿" onclick="saveAsDraft()">
			&nbsp;&nbsp;&nbsp;
			<input type="button" class="com_btn" value="立即发布" onclick="saveAsPublish()">
			&nbsp;
		</p>
	</div>
	<div class="clear"></div>
</form>
</div>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="/static/lib/ke/kindeditor-min.js"></script>
<script type="text/javascript" src="/static/lib/ke/lang/zh_CN.js"></script>

<link rel="stylesheet" type="text/css" href="/static/lib/qtip/jquery.qtip.min.css">
<script type="text/javascript" src="/static/lib/qtip/jquery.qtip.min.js"></script>

<#-- 由于KE定义插件不是太方便, 用ftl方式来做稍微好点 -->
<#include "/static/js/ke-quote.ftl">

<script type="text/javascript">
	var contentEditor;
	var abstractsEditor;
	KindEditor.ready(function(K) {
		contentEditor = K.create('textarea[name="content"]', {
			cssPath: "/static/css/ke-miniblog4j.css",
			uploadJson: "/admin/upload",
			fileManagerJson: "/admin/file?oper=manage",
			allowFileManager: true,
			resizeType: 1,
			items: ['bold', 'italic', 'underline', 'strikethrough', 'removeformat', 'clearhtml', '|',
				'forecolor', 'hilitecolor', 'formatblock', 'fontname', 'fontsize', '|', 
				'justifyleft', 'justifycenter', 'justifyright',
		        'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', '|', 
		        'image', 'flash', 'media', 'insertfile', 'emoticons', 'map', '/',
		        'code', 'quote', 'link', 'unlink', 'table', 'hr','|',
		        'selectall', 'source', 'fullscreen', 'about']
		});
		
		abstractsEditor = K.create('textarea[name="abstracts"]', {
			cssPath: "/static/css/ke-miniblog4j.css",
			uploadJson: "/admin/upload",
			fileManagerJson: "/admin/file?oper=manage",
			allowFileManager: true,
			resizeType: 1,
			items: ['bold', 'italic', 'underline', 'strikethrough', 'removeformat', '|',
				'template', 'insertorderedlist', 'insertunorderedlist','|', 
				'forecolor', 'hilitecolor', 'fontname', 'fontsize',  '|', 
				'link', 'unlink', 'emoticons', 'code', 'image', 'flash', '|', 
				'selectall', 'source', 'about']
		});
	});
	
	$(function() {
		$("input[name=status]").click(function() {
			var val = $(this).val();
			if (val === "protected") {
				$("[name=password]").show();
			} else {
				$("[name=password]").hide();
			}
		});
	});
	
	// 打开或者关闭 选择常用标签
	function tags() {
		$("#taglist").toggle();
	}
	
	// 添加新标签按钮事件
	function addnewtag() {
		var ntag = $.trim($("[name=newtag]").val());
		insertNewTag(ntag);
	}
	
	// 添加新标签
	function insertNewTag(ntag) {
		if (ntag === "") return;
		var ntags = ntag.split(",");
		for (var i = 0; i < ntags.length; i++) {
			var tag = $.trim(ntags[i]);
			if (!checkTag(tag)) {
				var html = "<li class=\"r\"><input type=\"hidden\" name=\"tag\" value=\"" 
					+ tag 
					+ "\"><a class=\"del\" onclick=\"del(this)\">&nbsp;</a>&nbsp;" 
					+ tag 
					+ "</li>";
					
				$("#tags").append(html);
			}
		}
		
		$("[name=newtag]").val("");
	}
	
	// 检测是否已经存在此标签, 若有返回true
	function checkTag(tag) {
		var flag = false;
		$("#tags li").each(function(i, n){
			if (tag === $.trim($(this).text())) {
				flag = true;
			}
			return !false;
		});
		
		return flag;
	}
	
	// 删除图标点击
	function del(o) {
		$(o).parent().remove();
	}
	
	// 存为草稿点击
	function saveAsDraft() {
		$("[name=saveStatus]").val("draft");
		ajaxSaveForm();
	}
	
	// 立即发布点击
	function saveAsPublish() {
		$("[name=saveStatus]").val("publish");
		ajaxSaveForm();
	}
	
	// 自动保存
	function saveAsAuto() {
		$("[name=saveStatus]").val("auto");
		ajaxSaveForm();
	}
	
	// 显示提示消息
	function doTip(id, text) {
		$("#" + id).qtip({
		   content: text,
		   position: {
				my: "center left",
				at: "right center",
			},
		   show: { event: false, ready: true },
		   hide: { event: false, inactive: 6000 }
		});
	}
	
	// 验证
	function checkForm() {
		if ($("[name=title]").val() === "") {
			doTip("title_label", "请输入文章标题");
			$("#loading").hide();
			return false;
		}
		
		if ($("[name=content]").val() === "") {
			doTip("content_label", "请输入文章内容");
			$("#loading").hide();
			return false;
		}
		
		return true;
	}

	// 提交表单
	function ajaxSaveForm() {
		$("#loading").show();
		contentEditor.sync();
		abstractsEditor.sync();
		var form = $("#postForm");
		$.ajax({
			url: form.attr("action"),
			data: form.serialize(),
			type: "POST",
			beforeSend: checkForm,
			success: function(data) {
				$("#loading").hide();
				if (data.code && data.code == 1) {
					// 成功
					$("input[name=id]").val(data.id);
				}
				if (data.msg) {
					doTip("tip", data.msg);
				} else {
					doTip("tip", "保存失败!");
				}
			}
		});
	}
</script>